package com.yufei.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yufei.common.R;
import com.yufei.entity.Orders;
import com.yufei.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @GetMapping("/order/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        //分页构造器对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //构造条件查询对象
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.like(number != null, "number", number)
                .gt(StringUtils.hasText(beginTime), "order_time", beginTime)
                .lt(StringUtils.hasText(endTime), "order_time", endTime);

        ordersService.page(pageInfo, wrapper);

        return R.success(pageInfo);
    }

    @PutMapping("/order")
    public R<String> editOrderDetail(@RequestBody Orders orders, HttpServletRequest request) {
        System.out.println("进入到editOrderDetail方法，获取的orders为"+orders.toString());
        //todo 更新人
        boolean update = ordersService.updateById(orders);
        if (update) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

}
